package com.kstudio.ksnapshot.kimage;

import java.io.File;
import java.util.ArrayList;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.kstudio.ksnapshot.KSnapShotAsync;
import com.kstudio.ksnapshot.KSnapShotAsync.KSnapShotAsyncListener;
import com.kstudio.ksnapshot.KSnapshot;
import com.kstudio.ksnapshot.KUtil;
import com.kstudio.ksnapshot.R;
import com.kstudio.ksnapshot.kimageedit.KImageEditActivity;

public class KImageActivity extends Activity implements KSnapShotAsyncListener {
    KUtil log = new KUtil("KImageActivity");

    private Context kContext;
    private String kPath;

    private GridView kGrid;
    private ArrayList<KImageFileData> kFileList;
    private KImageAdapter kAdapter;
    private int kGridWidth;
    private int kGridHeight;

    private KSnapShotAsync kSnapShotAsync;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        log.i("***************** onCreate *****************");
        super.onCreate(savedInstanceState);

        setContentView(R.layout.kimage_activity);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);

        init();
    }

    @Override
    protected void onRestart() {
        log.i("***************** onRestart *****************");
        super.onRestart();
    }

    @Override
    protected void onStart() {
        log.i("***************** onStart *****************");
        super.onStart();
    }

    @Override
    protected void onResume() {
        log.i("***************** onResume *****************");
        super.onResume();
    }

    @Override
    protected void onPause() {
        log.i("***************** onPause *****************");
        super.onPause();
    }

    @Override
    protected void onStop() {
        log.i("***************** onStop *****************");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        log.i("***************** onDestroy *****************");
        super.onDestroy();
    }

    @Override
    public void OnProgressUpdate() {
        kAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class KOnItemClickListenr implements OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> adapterview, View view, int i, long l) {
            log.i("onItemClick %d",i);
            KImageFileData fileData = (KImageFileData) adapterview.getItemAtPosition(i);

            Intent intent = new Intent(kContext, KImageEditActivity.class);
            intent.putExtra("path", fileData.getPath());
            startActivity(intent);

        }
    }

    /* private */
    private void init() {
        kPath = ((KSnapshot) getApplicationContext()).getStoragePath();

        kContext = this;
        kSnapShotAsync = new KSnapShotAsync(this);

        kGrid = (GridView) findViewById(R.id.kgrid);
        final Bitmap b = BitmapFactory.decodeResource(kContext.getResources(), R.drawable.kimage_temp);
        kGridWidth = b.getWidth();
        kGridHeight = b.getHeight();
        b.recycle();
        kGrid.setColumnWidth(kGridWidth);

        kFileList = new ArrayList<KImageFileData>();
        kAdapter = new KImageAdapter(this, kFileList, kGridWidth, kGridHeight);

        KUpdateFileList(kPath);
        kGrid.setAdapter(kAdapter);
        kGrid.setOnItemClickListener(new KOnItemClickListenr());
    }

    private void KUpdateFileList(String path) {
        File[] files = new File(path).listFiles();
        String name;
        String extention;
        int extentionIndex;
        String[] PictureExtiontion = {"PNG","png","JPG","jpg","BMP","bmp"};

        kFileList.clear();

        for(File file: files) {
            name = file.getPath();
            extentionIndex = name.lastIndexOf(String.valueOf('.'));
            if(extentionIndex > 0) {
                extention = name.substring(extentionIndex + 1);
                for(String ex : PictureExtiontion) {
                    if(extention.equals(ex)) {
                        kFileList.add(new KImageFileData(file.getPath()));
                        break;
                    }
                }
            }
        }

        kSnapShotAsync.KStart(KSnapShotAsync.SNAPSHOT_GET_IMAGE_THUMBNAIL, (ArrayList<?>) kFileList, kGridWidth, kGridHeight);
    }
}
